package com.petmall.cart.controller;

import com.petmall.cart.service.CartService;
import com.petmall.cart.vo.CartItemVo;
import com.petmall.cart.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author liang zai
 * @date 2022/4/15 15:13
 * @description
 */
@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 远程调用：订单服务调用【更新最新价格】
     * 获取当前用户购物车所有选中的商品项check=true【从redis中取】
     */
    @GetMapping("/getCheckItems")
    @ResponseBody
    public List<CartItemVo> getCurrentCartItems() throws ExecutionException, InterruptedException {
        List<CartItemVo> cartItemVo = cartService.getCheckItems();
        return cartItemVo;
    }


    @GetMapping("cart.html")
    public String cartLogin(Model model) throws ExecutionException, InterruptedException {
        //快速得到用户信息   userid userkey
        CartVo cartVo = cartService.getCart();
        model.addAttribute("cart",cartVo);
        return "cartList";
    }

    //加入购物车

    /**
     *
     * @param skuId
     * @param num
     * @param ra RedirectAttributes
     *           ra.addFlashAttribute()  将数据保存在session中，可以在页面中取出数据，但是只能取一次
     *           ra.addAttribute()  将数据放在url后面
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     * 添加购物车
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId")Long skuId, @RequestParam("num")Integer num, RedirectAttributes ra) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId,num);
        ra.addAttribute("skuId",skuId);

        return "redirect:http://cart.petmall.com/addToCartSession.html";
    }

    /**
     * 查看购物车
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("addToCartSession.html")
    public String addToCartSession(@RequestParam("skuId")Long skuId,Model model){
        CartItemVo cartItem = cartService.getCartItem(skuId);
        model.addAttribute("item",cartItem);
        return "success";
    }

    /**
     * 更改购物车的选择状态
     * @param skuId
     * @param check
     * @return
     */
    @GetMapping("checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check") Integer check){
        cartService.checkItem(skuId,check);
        return "redirect:http://cart.petmall.com/cart.html";
    }

    /**
     * 修改购物车数量
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("countItem")
    public String countItem(@RequestParam("skuId") Long skuId,@RequestParam("num") Integer num){
        cartService.countItem(skuId,num);
        return "redirect:http://cart.petmall.com/cart.html";
    }

    /**
     * 删除购物车
     * @param skuId
     * @return
     */
    @GetMapping("deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.petmall.com/cart.html";
    }
}
